
//-----------------------------------------------------------------------
QY_INLINE void Vector2::Set( const float vx, const float vy )
{
	this->x = vx;
	this->y = vy;
}

//-----------------------------------------------------------------------
QY_INLINE void Vector2::Zero()
{
	x = y = 0;
}

//-----------------------------------------------------------------------
QY_INLINE float Vector2::operator[]( int index ) const
{
	return (&x)[index];
}

//-----------------------------------------------------------------------
QY_INLINE float & Vector2::operator[]( int index )
{
	return (&x)[index];
}

//-----------------------------------------------------------------------
QY_INLINE bool Vector2::Compare( const Vector2 &a, const float epsilon ) const
{
	if ( fabs( x - a.x ) > epsilon )
	{
		return false;
	}
	if ( fabs( y - a.y ) > epsilon )
	{
		return false;
	}
	return true;
}

//-----------------------------------------------------------------------
QY_INLINE bool Vector2::Compare( const Vector2 &a ) const
{
	return ( (x==a.x) && (y==a.y) );
}

//-----------------------------------------------------------------------
QY_INLINE bool Vector2::operator ==( const Vector2 &a ) const
{
	return Compare( a );
}

//-----------------------------------------------------------------------
QY_INLINE bool Vector2::operator !=( const Vector2 &a ) const
{
	return !Compare( a );
}

//-----------------------------------------------------------------------
QY_INLINE bool Vector2::IsZero() const
{
	return ( ( (*(const unsigned int *)&(x)) | (*(const unsigned int *)&(y)) ) & ~(1<<31) ) == 0;
}

//-----------------------------------------------------------------------
QY_INLINE float Vector2::Length() const
{
	return (float)sqrtf( x * x + y * y );
}

//-----------------------------------------------------------------------
QY_INLINE float Vector2::LengthFast() const
{
	float sqrLength;
	sqrLength = x * x + y * y;
	return sqrLength * qyRSqrt( sqrLength );
}

//-----------------------------------------------------------------------
QY_INLINE float Vector2::LengthSqr() const
{
	return ( x * x + y * y );
}

//-----------------------------------------------------------------------
QY_INLINE float Vector2::Normalize()
{
	float invLength, length;

	length = x * x + y * y;
	length = sqrtf( length );
	if ( length < FLT_EPSILON ) 
	{
		return 0.f;
	}
	invLength = 1.f / length;
	x *= invLength;
	y *= invLength;
	return length;
}

//-----------------------------------------------------------------------
QY_INLINE float Vector2::NormalizeFast()
{
	float lengthSqr, invLength;

	lengthSqr = x * x + y * y;
	invLength = qyRSqrt( lengthSqr );
	x *= invLength;
	y *= invLength;
	return invLength * lengthSqr;
}

//-----------------------------------------------------------------------
QY_INLINE Vector2 & Vector2::Truncate( float length )
{
	float length2;
	float ilength;

	if ( !length ) 
	{
		Zero();
	}
	else 
	{
		length2 = LengthSqr();
		if ( length2 > length * length ) 
		{
			ilength = length * qyRSqrt( length2 );
			x *= ilength;
			y *= ilength;
		}
	}

	return *this;
}

//-----------------------------------------------------------------------
QY_INLINE void Vector2::Clamp( const Vector2 &min, const Vector2 &max )
{
	if ( x < min.x ) 
	{
		x = min.x;
	} else if ( x > max.x ) 
	{
		x = max.x;
	}
	if ( y < min.y ) 
	{
		y = min.y;
	} else if ( y > max.y ) 
	{
		y = max.y;
	}
}

//-----------------------------------------------------------------------
QY_INLINE void Vector2::Snap()	
{
	x = floorf( x + 0.5f );
	y = floorf( y + 0.5f );
}

//-----------------------------------------------------------------------
QY_INLINE void Vector2::SnapInt()
{
	x = floorf( x );
	y = floorf( y );
}

//-----------------------------------------------------------------------
QY_INLINE Vector2 Vector2::operator-() const
{
	return Vector2( -x, -y );
}

//-----------------------------------------------------------------------
QY_INLINE Vector2 Vector2::operator -( const Vector2 &a ) const
{
	return Vector2( x - a.x, y - a.y );
}

//-----------------------------------------------------------------------
QY_INLINE float Vector2::operator*( const Vector2 &a )  const
{
	return x * a.x + y * a.y;
}

//-----------------------------------------------------------------------
QY_INLINE Vector2 Vector2::operator*( const float a ) const
{
	return Vector2( x * a, y * a );
}

//-----------------------------------------------------------------------
QY_INLINE Vector2 Vector2::operator/( const float a ) const
{
	const float inva = 1.0f / a;
	return Vector2( x * inva, y * inva );
}

//-----------------------------------------------------------------------
QY_INLINE Vector2 operator*( const float a, const Vector2 &b )
{
	return Vector2( a * b.x, a * b.y );
}

//-----------------------------------------------------------------------
QY_INLINE Vector2 Vector2::operator+( const Vector2 &a ) const
{
	return Vector2( x + a.x, y + a.y );
}

//-----------------------------------------------------------------------
QY_INLINE Vector2 & Vector2::operator+=( const Vector2 &a )
{
	x += a.x;
	y += a.y;
	return (*this);
}

//-----------------------------------------------------------------------
QY_INLINE Vector2 & Vector2::operator /=( const Vector2 &a )
{
	x /= a.x;
	y /= a.y;
	return (*this);
}

//-----------------------------------------------------------------------
QY_INLINE Vector2 & Vector2::operator /=( const float a )
{
	float inva = 1.0f / a;
	x *= inva; y *= inva;
	return (*this);
}

//-----------------------------------------------------------------------
QY_INLINE Vector2 & Vector2::operator-=( const Vector2 &a )
{
	x -= a.x;
	y -= a.y;
	return (*this);
}

//-----------------------------------------------------------------------
QY_INLINE Vector2 & Vector2::operator*=( const float a )
{
	x *= a;
	y *= a;
	return *this;
}

//-----------------------------------------------------------------------
QY_INLINE Vector2 & Vector2::operator =( const Vector2 &a )
{
	x = a.x;
	y = a.y;
	return (*this);
}

//-----------------------------------------------------------------------
QY_INLINE int Vector2::GetDimension() const
{
	return 2;
}

//-----------------------------------------------------------------------
QY_INLINE const float * Vector2::ToFloatPtr() const
{
	return &x;
}

//-----------------------------------------------------------------------
QY_INLINE float * Vector2::ToFloatPtr()
{
	return &x;
}


